package com.topscomm.pm.script;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.cbo.service.impl.CboUserService;
import com.topscomm.pm.PmScript;
import com.topscomm.pm.service.impl.PmProjectService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

public class PmProjectScript extends PmScript {
    /**
     * 立项审批结束后回写项目信息
     *
     * @param service
     * @param entity
     * @throws Exception
     */
    @Transactional
    public void projectApproval(ITapService service, TapEntity entity) throws Exception {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        PmProjectService pmProjectService = (PmProjectService) SpringUtil.getBean("pmProjectService");
        //审批人id
        long approvaluserid = nodeins.getApprovaluserid();
        //查询审批人信息
        CboUserService cboUserService = (CboUserService) SpringUtil.getBean("cboUserService");
        CboUserEntity approveUser = cboUserService.queryById(approvaluserid + "");
        //更新项目信息表立项审批通过的相关字段
        Map<String, Object> updateParam = new HashedMap<>();
        updateParam.put("id", sourceid);
        updateParam.put("approvalcode", approveUser.getCode());
        updateParam.put("approvalname", approveUser.getName());
//        updateParam.put("projectstate", 1);
        updateParam.put("approvaltime", new Date());
        pmProjectService.update(updateParam);

        // 立项通过后生成项目日历
//        PmCalendarService pmCalendarService = (PmCalendarService) SpringUtil.getBean("pmCalendarService");
//        PmProjectEntity pmProjectEntity = pmProjectService.queryById(sourceid);
//        long projectId = 0L;
//        String projectCode = "";
//        Date planStartTime = null;
//        Date planEndTime = null;
//        if (pmProjectEntity != null) {
//            projectId = pmProjectEntity.getId();//项目id
//            projectCode = pmProjectEntity.getCode();//项目号
//            planStartTime = pmProjectEntity.getPlanstarttime();//计划完工时间
//            planEndTime = pmProjectEntity.getPlanendtime();//计划完工时间
//        }
//        Map<String, Object> map = new HashMap<>();
//        map.put("projectid", projectId);
//        map.put("projectcode", projectCode);
//        map.put("starttime", planStartTime);
//        map.put("endtime", planEndTime);
//        pmCalendarService.insertBatchCalendar(map);
    }
}
